package com.colorlight.cloud.complete.entity.instructions;

import com.colorlight.cloud.common.exception.ParameterException;
import com.colorlight.cloud.common.model.ExceptionEnum;
import com.colorlight.cloud.complete.api.CompleteEntityScheduleActions;
import com.colorlight.cloud.complete.entity.po.AuthorizationDetail;
import com.colorlight.cloud.complete.entity.po.DeviceAuthResultInfo;
import com.colorlight.cloud.complete.entity.po.SenderAuthResultInfo;
import com.colorlight.iot.proto.complete.instruction.CompleteInstruction;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 加密下发指令
 * 加密下发指令以及返回信息，设备主体和发送卡加解密通用接口
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Slf4j
public class Encrypt implements CompleteEntityScheduleActions {

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    public static class InputMsg implements Serializable {
        /**
         * 授权详情
         */
        private AuthorizationDetail detail;
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    public static class OutputMsg implements Serializable {
        /**
         * 设备授权结果信息
         */
        private DeviceAuthResultInfo deviceAuthResultInfo;
        /**
         * 发送卡授权结果信息列表
         */
        private List<SenderAuthResultInfo> senderAuthResultInfoList;
    }

    private InputMsg inputMsg;
    private OutputMsg outputMsg;

    @Override
    public ByteString objectConvertBytes() {
        final CompleteInstruction.Encrypt.InputMsg.Builder inputMsgBuilder =
                CompleteInstruction.Encrypt.InputMsg.newBuilder();
        Encrypt.InputMsg thisInput = this.getInputMsg();
        inputMsgBuilder.setDetail(thisInput.getDetail().toProto());

        final CompleteInstruction.Encrypt.Builder builder =
                CompleteInstruction.Encrypt.newBuilder();
        builder.setInput(inputMsgBuilder);
        return builder.build().toByteString();
    }

    @Override
    public Encrypt byteConvertObject(ByteString bytes) {
        CompleteInstruction.Encrypt encrypt = null;
        try {
            encrypt = CompleteInstruction.Encrypt.parseFrom(bytes);
        } catch (InvalidProtocolBufferException e) {
            log.error("", e);
        }
        if (Objects.isNull(encrypt)) {
            throw new ParameterException(ExceptionEnum.MSG_PARAMETER_EXCEPTION);
        }
        Encrypt.OutputMsg outputMsg = new Encrypt.OutputMsg();
        outputMsg.setDeviceAuthResultInfo(new DeviceAuthResultInfo().formProto(encrypt.getOutput().getDeviceAuthResultInfo()));
        List<SenderAuthResultInfo> senderAuthResultInfoList = encrypt.getOutput().getSenderAuthResultInfoList().stream()
                .map(p -> new SenderAuthResultInfo().formProto(p)).collect(Collectors.toList());
        outputMsg.setSenderAuthResultInfoList(senderAuthResultInfoList);
        this.setOutputMsg(outputMsg);
        return this;
    }
}

